package com.tianchi.lexiang.util;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.tianchi.lexiang.bean.PublicSong;

public class PublicSongQueryUtil {
	private static final String TAG = "PublicSongQueryUtil";
	private static PublicSongQueryUtil instance = new PublicSongQueryUtil();
	private SQLiteDatabase db = DBOperator.getInstance().getConnection();
	
	private PublicSongQueryUtil(){}
	
	public static PublicSongQueryUtil getInstance(){
		return instance;
	}
	
	public PublicSong queryNextPublicSong(int currentId){
		String sql = "select * from T_BDSM_PUBLIC_SONG order by "+PublicSong.COLUMN_ID;
		Cursor mCursor = db.rawQuery(sql, null);
		Log.d(TAG, "queryNextPublicSong sql==>"+sql);
		int count = mCursor.getCount();
		if (count == 0) {
			Log.e(TAG, "publicsong table has no songs,it's an error");
			mCursor.close();
			return null;
		}
		if (currentId >= count ) {
			currentId = 0;
			Log.d(TAG, "it's the end song in public song table,so restart from the first song");
		}
		int idIndex = mCursor.getColumnIndex(PublicSong.COLUMN_ID);
		Log.d(TAG, "queryNextPublicSong idIndex==>"+idIndex);
		PublicSong publicSong = new PublicSong();
		while (mCursor.moveToNext()) {
			int id = mCursor.getInt(idIndex);
			if (id > currentId) {
				publicSong.id = id;
				publicSong.songId = mCursor.getInt(mCursor.getColumnIndex(PublicSong.COLUMN_SONG_ID));
				break;
			}
		}
		Log.d(TAG, "next publicsong ==>"+publicSong);
		mCursor.close();
		return publicSong;
	}
	
}
